﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Runtime.InteropServices;

namespace SP2010.Enhancements.Management
{
    class SPFeatureManager
    {        
        private SPFeatureDefinition getSPFeatureDefinition(SPSite Site, Guid id)
        {
            try
            {
                return Site.FeatureDefinitions[id];
            }
            catch
            {
                throw;
            }
        }
        private SPFeature getSPFeature(SPSite Site, Guid id)
        {
            try
            {
                return Site.Features[id];
            }
            catch
            {
                throw;
            }
        }
        private void processSPWebScopeFeature(SPSite Site, SPWeb Web, Guid id)
        {
            try
            {
                SPFeatureCollection featureColl = Web.Features;
                bool matched = matchFeature(featureColl, id);
                if (matched)
                {

                    string version = Web.Features[id].Version.ToString();
                    string versionDef = Web.Features[id].Definition.Version.ToString();
                    if (featureNeedsToBeUpgraded(versionDef, version))
                    {
                        //Upgrade
                        //Do not upgrade
                    }
                }
            }
            catch
            {
                throw;
            }
        }
        private void upgradeWebFeature(SPWeb Web, Guid id)
        {
            try
            {
                Web.Features[id].Upgrade(true);
            }
            catch
            {
                throw;
            }

        }
        internal bool matchFeature(SPFeatureCollection featureColl, Guid id)
        {
            bool matched = false;
            try
            {            
                foreach (SPFeature feature in featureColl)
                {
                    if (feature.DefinitionId.ToString() == id.ToString())
                    {
                        matched = true;
                    }
                }           
            }
            catch
            {
                throw;
            }
            return matched;
        }
        private bool featureNeedsToBeUpgraded(string versionDef, string version)
        {
            try
            {
                if (versionDef == version) return false;
                else return true;
                }
            catch
            {
                throw;
            }  
        }
        private void processSPSiteScopeFeature(SPSite Site, Guid id)
        {
            try
            {
                SPFeatureCollection featureColl = Site.Features;
                bool matched = matchFeature(featureColl, id);
                if (matched)
                {

                    string version = Site.Features[id].Version.ToString();
                    string versionDef = Site.Features[id].Definition.Version.ToString();
                    if (featureNeedsToBeUpgraded(versionDef, version))
                    {
                        //Upgrade
                        //Do not upgrade
                    }
                    else
                    { }
                }
                else
                { }
            }
            catch
            {
                throw;
            }  
        }              
    }
}
